<!--
 * @Author: Dweriout 2428458948@qq.com
 * @Date: 2022-06-10 09:52:18
 * @LastEditors: Dweriout 2428458948@qq.com
 * @LastEditTime: 2022-06-10 18:02:11
 * @FilePath: \xshop2d:\workProject\nien-vue3-admin\src\views\BusinessConfiguration\costPriceManagement\components\PriceDetailDialog.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-dialog
    v-model="openPrice"
    :close-on-click-modal="false"
    title="价格详情"
  >
    <el-button type="primary" :icon="Plus" @click="edit('新增')">新增</el-button>
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="状态" align="center" prop="status">
        <template #default="{row}">
          <el-switch
            :value="row.status"
            :active-value="1"
            :inactive-value="0"
            @change="val => handelChangeSwitch(row, val)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="生效日期" align="center" prop="openingDate"></el-table-column>
      <el-table-column label="失效日期" align="center" prop="obsoleteDate"></el-table-column>
      <el-table-column label="备注" align="center" prop="remarks"></el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="{row}">
          <el-button type="primary" text @click="edit('修改',row)">修改</el-button>
          <el-divider direction="vertical" class="divider-margin"></el-divider>
          <el-button type="primary" text @click="editDelete(row.id)">删除</el-button>
          <el-divider direction="vertical" class="divider-margin"></el-divider>
          <el-button type="primary" text @click="priceManage(row)">价格管理</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--新增修改价格管理  -->
    <el-dialog
      v-model="editVisible"
      :close-on-click-modal="false"
      :title="`${editTitle}价格管理`"
    >
      <el-form
        ref="editFormRef"
        :model="editForm"
        label-width="100px"
        :rules="rules"
        inline
      >
        <el-form-item label="生效日期" prop="openingDate">
          <el-date-picker
            v-model="editForm.openingDate"
            type="datetime"
            placeholder="选择日期"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="失效日期" prop="obsoleteDate">
          <el-date-picker
            v-model="editForm.obsoleteDate"
            type="datetime"
            placeholder="选择日期时间"
            clearable
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input
            v-model="editForm.remarks"
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button type="primary" :loading="editLoading" @click="editConfirm">确定</el-button>
          <el-button @click="editCancel">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </el-dialog>
</template>
<script lang="ts" setup>
import { ref, nextTick } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import { ElButton, ElMessageBox, ElMessage, ElSwitch } from 'element-plus'
import { getcostList, getcostDetail, addCostManagement, editCostManagement, delCostManagement } from '@/api/BusinessConfig/costPriceManagement'
import { validateForm, depClone } from '@/utils'

const openPrice = ref<boolean>(false)
const loading = ref<boolean>(false)
const tableData = ref([])
const costPriceId = ref(null)
const editVisible = ref<boolean>(false)
const getList = () => {
  loading.value = true
  const data = { pageSize: 10, pageNum: 1, costPriceId: costPriceId.value }
  getcostList(data).then((({ code, data }) => {
    if (code === 200) {
      tableData.value = data[0]
    }
  })).finally(() => {
    loading.value = false
  })
}
/* 新增------编辑------删除 start */
const editLoading = ref<boolean>(false)
const editTitle = ref<string>('修改')
const editFormRef = ref()
const editForm = ref({
  openingDate: '',
  obsoleteDate: '',
  remarks: '',
})
const rule = [{ required: true, message: '不能为空', trigger: 'change' }]
const rules = {
  openingDate: rule,
  obsoleteDate: rule,
  remarks: rule,
}
const editDelete = (id) => {
  ElMessageBox.confirm('是否删除当前项', '警告', {
    type: 'warning',
  }).then(() => {
    delCostManagement(id).then(res => {
      if (res.code === 200) {
        ElMessage.success('操作成功')
        getList()
      }
    })
  })
}
const handelChangeSwitch = (row, val) => {
  ElMessageBox.confirm('是否修改当前状态', '警告').then(() => {
    const data = {
      id: row.id,
      status: val,
    }
    editCostManagement(data).then((res) => {
      if (res.code === 200) {
        row.status = val
        ElMessage.success('修改成功')
      }
    })
  })
}
const edit = (type:string, row?) => {
  editTitle.value = type
  editVisible.value = true
  if (type === '修改') {
    editForm.value = depClone(row)
  }
  nextTick(() => {
    editFormRef.value && editFormRef.value.resetFields()
  })
}
const editConfirm = () => {
  validateForm(editFormRef.value, () => {
    const fn = editTitle.value === '修改' ? editCostManagement : addCostManagement
    editLoading.value = true
    const data = {
      ...editForm.value,
      costPriceId: costPriceId.value,
    }
    fn(data).then((res) => {
      if (res.code === 200) {
        ElMessage.success(`${ editTitle.value }成功！`)
        editCancel()
        getList()
      }
    }).finally(() => {
      editLoading.value = false
    })
  })
}
const editCancel = () => {
  editForm.value = {
    openingDate: '',
    obsoleteDate: '',
    remarks: '',
  }
  editVisible.value = false
}
/* 新增------编辑-----删除 end */
/* 价格管理 */
const priceManage = (row) => {

}
const open = (id) => {
  costPriceId.value = id
  openPrice.value = true
  getList()
}
defineExpose({
  open,
})
</script>